package com.sx.baseproject.framework.net.base;


import android.annotation.SuppressLint;
import android.text.TextUtils;

import androidx.annotation.NonNull;

import com.example.mvvm.rx.RxUtils;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.cache.CacheMode;
import com.lzy.okgo.request.GetRequest;
import com.lzy.okgo.request.PostRequest;
import com.lzy.okgo.request.base.Request;

import java.io.File;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import io.reactivex.Observable;
import io.reactivex.ObservableEmitter;
import io.reactivex.ObservableOnSubscribe;

@SuppressLint("CheckResult")
public class MyOkGo {
    protected static MyOkGo myOkGo;


    public static MyOkGo getInstance() {
        if (myOkGo == null) {
            synchronized (MyOkGo.class) {
                if (myOkGo == null) {
                    myOkGo = new MyOkGo();
                }
            }
        }
        return myOkGo;
    }

    /*这个是没有缓存的*/
    public <T> Observable<T> RxHttp(String api, BaseRequest baseRequest, Class mClass) {
        return RxHttp(api, baseRequest, mClass, false);
    }

    public <T> Observable<T> RxHttp(String api, Class mClass) {
        return RxHttp(api, new GetBaseRequest(), mClass, false);
    }

    public <T> Observable<T> RxHttp(PostRequest postRequest, Class<T> mClass,FileProgress fileProgress) {
        return Observable.create(new ObservableOnSubscribe<T>() {
            @Override
            public void subscribe(@io.reactivex.annotations.NonNull ObservableEmitter<T> emitter) throws Exception {
                postRequest.execute(new ResponseCallBack(emitter, mClass,fileProgress));
            }
        }).compose(RxUtils.schedulersTransformer())
                .compose(RxUtils.exceptionTransformer());
    }

    public <T> Observable<T> RxHttp(String api, BaseRequest baseRequest, Class mClass, boolean cache,CacheMode cacheMode) {
        return Observable.create(new ObservableOnSubscribe<T>() {
            @Override
            public void subscribe(@NonNull ObservableEmitter<T> emitter) throws Exception {
                Request request;
                if (baseRequest.isGet) {
                    request = getRequest(api, (GetBaseRequest) baseRequest);
                } else {
                    request = postRequest(api, (PostBaseRequest) baseRequest);
                }
                if (cache) {
                    request = request.cacheMode(cacheMode);
                }
                request.execute(new ResponseCallBack(emitter, mClass,cacheMode));
            }
        }).compose(RxUtils.schedulersTransformer())
                .compose(RxUtils.exceptionTransformer());
    }

    public <T> Observable<T> RxHttp(String api, BaseRequest baseRequest, Class mClass, boolean cache) {
        return Observable.create(new ObservableOnSubscribe<T>() {
            @Override
            public void subscribe(@NonNull ObservableEmitter<T> emitter) throws Exception {
                Request request;
                if (baseRequest.isGet) {
                    request = getRequest(api, (GetBaseRequest) baseRequest);
                } else {
                    request = postRequest(api, (PostBaseRequest) baseRequest);
                }
                if (cache) {
                    request = request.cacheMode(CacheMode.FIRST_CACHE_THEN_REQUEST);
                }
                request.execute(new ResponseCallBack(emitter, mClass,CacheMode.FIRST_CACHE_THEN_REQUEST));
            }
        }).compose(RxUtils.schedulersTransformer())
                .compose(RxUtils.exceptionTransformer());
    }


    public PostRequest postRequest(String api, PostBaseRequest baseRequest) {
        return OkGo.<String>post(api + baseRequest.toGetParam())
                .upRequestBody(baseRequest.toBody());
    }
    public PostRequest postRequest(String api, String name, File file) {
        return OkGo.<String>post(api)
                .params(name, file)
                .isMultipart(true);
    }
    public GetRequest getRequest(String api, GetBaseRequest baseRequest) {
        return OkGo.<String>get(api).params(baseRequest.toGetParam());
    }



}
